package com.topcoder.srm515;

import java.util.Arrays;
import java.util.HashSet;

public class FortunateNumbers {
	public int getFortunate(int[] a, int[] b, int[] c) {
		Arrays.sort(a);
		Arrays.sort(b);
		Arrays.sort(c);
		HashSet<Integer> h = new HashSet<Integer>();
		for (int i=0; i<a.length; i++) {
			if (i>0 && a[i] == a[i-1]) continue;
			for (int j=0; j<b.length; j++) {
				if (j>0 && b[j] == b[j-1]) continue;
				for (int k=0; k<c.length; k++) {
					if (k>0 && c[k] == c[k-1]) continue;
					int t = a[i] + b[j] + c[k];
					boolean fortunate = true;
					while (t > 0) {
						if (t%10 != 5 && t%10 != 8) {
							fortunate = false;
							break;
						}
						t = t/10;
					}
					if (fortunate) h.add(a[i] + b[j] + c[k]);
				}
			}
		}
		
		return h.size();
	}
}
